<template>
  <el-date-picker class="picker-wrap"
                  v-model="curryData"
                  type="daterange"
                  align="right"
                  :disabled="isDisabled"
                  start-placeholder="起始时间"
                  end-placeholder="结束时间"
                  :default-time="['00:00:00', '23:59:59']"
                  @change="callback"
                  @onClick="onClick"
                  :picker-options="datePickerOptions"></el-date-picker>
</template>

<script>
import { formatDate } from "@/libs/util";
// import { numberFilters } from "../../views/NewPms/mixin";
export default {
  name: "DatePicker",
  props: {
    startDate: String,
    endDate: String,
    isUse: {
      type: Boolean,
      default: false,
    },
    isDisabled: {
      type: Boolean,
      default: false,
    },
    isshortcut: {
      type: Boolean,
      default: false,
    },
    loadingStyle: {
      type: Boolean,
      default: false,
    },
    isCurTime: Boolean,

    //ui控件 禁用 大于*月的不能选，传数字 *月
    disableMonths: Number,
  },
  data () {
    return {
      curryData: [this.startDate, this.endDate],
      beginTime: null,
      overTime: null,
      minDate: null,
      datePickerOptions: {
        onPick: ({ minDate }) => {
          if (this.disableMonths) {
            this.minDate = minDate;
          }
        },

        disabledDate: (time) => {
          if (this.disableMonths) {
            if (this.minDate) {
              // 大于的时间=            最小时间(onPick获取) + months
              let bigDay = this.$moment(this.minDate).add({
                months: this.disableMonths,
              });

              return bigDay + 8.64e7 <= time || time < this.minDate; //注意是||不是&&
            }
          }
        },
      },
    };
  },
  mounted () {
    if (this.isCurTime) {
      let startTime = new Date(
        new Date().setTime(new Date().getTime() - 3600 * 1000 * 24 * 30)
      );
      let endTime = new Date();
      let currentMonth, currentDay;
      let month =
        (currentMonth = startTime.getMonth() + 1) > 9
          ? currentMonth
          : "0" + currentMonth;
      let day =
        (currentDay = startTime.getDate()) > 9 ? currentDay : "0" + currentDay;
      let endMo, endDay;
      let endMonth = (endMo = endTime.getMonth() + 1) > 9 ? endMo : "0" + endMo;
      let endDays = (endDay = endTime.getDate()) > 9 ? endDay : "0" + endDay;
      this.$emit(
        "update:startDate",
        startTime.getFullYear() + "-" + month + "-" + day + ` 00:00:00`
      );
      this.$emit(
        "update:endDate",
        endTime.getFullYear() + "-" + endMonth + "-" + endDays + ` 23:59:59`
      );
    }
  },
  methods: {
    callback (val) {
      // 清掉之前选择的最小时间
      this.minDate = null;

      this.$emit(
        "update:startDate",
        !val
          ? ""
          : formatDate(val[0], "-") +
          ` ${this.filterTime(val[0].getHours())}:${this.filterTime(
            val[0].getMinutes()
          )}:${this.filterTime(val[0].getSeconds())}`
      );
      // this.$emit(
      //   'update:endDate',
      //   !val
      //     ? ''
      //     : formatDate(val[1], '-') +
      //         ` ${this.filterTime(val[1].getHours())}:${this.filterTime(
      //           val[1].getMinutes()
      //         )}:${this.filterTime(val[1].getSeconds())}`
      // )
      // 输入年月日时，结束时间的时分秒为 23:59:59
      this.$emit(
        "update:endDate",
        !val ? "" : formatDate(val[1], "-") + ` 23:59:59`
      );
    },
    // 父组件调用子组件方法清空日期组件
    filterTime (val) {
      return val > 9 ? val : `0${val}`;
    },
    clearTime () {
      this.curryData = ["", ""];
      this.$emit("update:startDate", "");
      this.$emit("update:endDate", "");
    },
    onClick () { },
    tipMsg () {
      if (this.dataTime() == false) {
        this.$message("最多可查询跨度为一年的数据");
        this.$emit("update:loadingStyle", false);
        return;
      }
      this.$emit("on-updata");
    },
    dataTime () {
      let time1 = new Date(this.startDate);
      let time2 = new Date(this.endDate);
      let diffyear = time2.getFullYear() - time1.getFullYear();
      let diffmonth = time2.getMonth() - time1.getMonth();
      // let diffDay = time2.getDate() - time1.getDate();
      if (diffyear === 0) {
        return true;
      } else if (diffyear === 1) {
        if (diffmonth > 0) {
          return false;
        }
        return true;
      }
      return false;
    },
  },
  watch: {
    // endDate: {
    //   immediate: true,
    //   handler() {
    //     this.curryData = [this.startDate, this.endDate];
    //     if (this.$store.state.route.system === "rms") {
    //       this.beginTime = this.curryData[0].split(" ")[0].replace(/-/g, "");
    //       this.overTime = this.curryData[1].split(" ")[0].replace(/-/g, "");
    //     }
    //   }
    // },
    // startDate: {
    //   immediate: true,
    //   handler() {
    //     this.curryData = [this.startDate, this.endDate];
    //     if (this.$store.state.route.system === "rms") {
    //       this.beginTime = this.curryData[0].split(" ")[0].replace(/-/g, "");
    //       this.overTime = this.curryData[1].split(" ")[0].replace(/-/g, "");
    //     }
    //   }
    // }
  },
};
</script>
<style scoped>
::v-deep .el-date-editor--daterange.el-input,
.el-date-editor--daterange.el-input__inner,
.el-date-editor--timerange.el-input,
.el-date-editor--timerange.el-input__inner {
  max-width: 230px !important;
}
</style>
